﻿using System.Collections.Generic;
using System.Data.SqlClient;

using BLL.Services.Categories.Listing;

namespace BLL.Services.Categories
{
    public class CategoriesService : ICategoriesService
    {
        private readonly CategoryCollection _categoriesCollection;

        public CategoriesService(CategoryCollection collection)
        {
            _categoriesCollection = collection;
        }

        public IEnumerable<Category> GetAllCategories
        {
            get
            {
                return _categoriesCollection.SelectAll();
            }
        }

        public Category GetCategoryById(int categoryId)
        {
            return _categoriesCollection.GetById(categoryId);
        }

        public void Create(Category category)
        {
            try
            {
                _categoriesCollection.Create(category);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Save(Category category)
        {
            try
            {
                _categoriesCollection.Save(category);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Delete(int categoryId)
        {
            try
            {
                _categoriesCollection.Delete(categoryId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public bool ValidateCategory(int categoryId, string name)
        {
            try
            {
                return _categoriesCollection.Validate(categoryId, name);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public CategoryListing GetCategoryListing(
            int page, int size, string column, int direction, CategoryFilter filter)
        {
            return _categoriesCollection.GetCategoryListing(page, size, column, direction, filter);
        }
    }
}
